<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>工厂模式</title>
  </head>
  <body>
    <script>
      // 工厂函数: 调用这个函数, 就能返回一个新的特定类型的实例对象
      function factory(role) {
        function User(options) {
          this.name = options.name
          this.viewPages = options.viewPages
        }
        switch (role) {
          case 'superAdmin':
            return new User({
              name: 'superAdmin',
              viewPages: ['首页', '发现页', '通讯录', '应用数据', '权限管理'],
            });
            break;
          case 'admin':
            return new User({
              name: 'admin',
              viewPages: ['首页', '发现页', '通讯录', '应用数据'],
            });
            break;
          case 'normal':
            return new User({
              name: 'normal',
              viewPages: ['首页', '发现页', '通讯录'],
            });
          default:
            throw new Error('角色名称不正确!');
        }
      }

      let superAdmin = factory('superAdmin')
      console.log(superAdmin)

      let admin = factory('admin')
      console.log(admin)

      let normal = factory('normal')
      console.log(normal)

      function fn() {
        return new Student()
      }

    </script>
  </body>
</html>
